package org.dromara.agriculture.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.agriculture.domain.vo.AgricultureMaterialTypeVo;
import org.dromara.agriculture.domain.bo.AgricultureMaterialTypeBo;
import org.dromara.agriculture.service.IAgricultureMaterialTypeService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 农资类别
 *
 * @author Robin
 * @date 2024-12-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/agriculture/materialType")
public class AgricultureMaterialTypeController extends BaseController {

    private final IAgricultureMaterialTypeService agricultureMaterialTypeService;

    /**
     * 查询农资类别列表
     */
    @SaCheckPermission("agriculture:materialType:list")
    @GetMapping("/list")
    public TableDataInfo<AgricultureMaterialTypeVo> list(AgricultureMaterialTypeBo bo, PageQuery pageQuery) {
        return agricultureMaterialTypeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出农资类别列表
     */
    @SaCheckPermission("agriculture:materialType:export")
    @Log(title = "农资类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AgricultureMaterialTypeBo bo, HttpServletResponse response) {
        List<AgricultureMaterialTypeVo> list = agricultureMaterialTypeService.queryList(bo);
        ExcelUtil.exportExcel(list, "农资类别", AgricultureMaterialTypeVo.class, response);
    }

    /**
     * 获取农资类别详细信息
     *
     * @param materialTypeId 主键
     */
    @SaCheckPermission("agriculture:materialType:query")
    @GetMapping("/{materialTypeId}")
    public R<AgricultureMaterialTypeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long materialTypeId) {
        return R.ok(agricultureMaterialTypeService.queryById(materialTypeId));
    }

    /**
     * 新增农资类别
     */
    @SaCheckPermission("agriculture:materialType:add")
    @Log(title = "农资类别", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AgricultureMaterialTypeBo bo) {
        return toAjax(agricultureMaterialTypeService.insertByBo(bo));
    }

    /**
     * 修改农资类别
     */
    @SaCheckPermission("agriculture:materialType:edit")
    @Log(title = "农资类别", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AgricultureMaterialTypeBo bo) {
        return toAjax(agricultureMaterialTypeService.updateByBo(bo));
    }

    /**
     * 删除农资类别
     *
     * @param materialTypeIds 主键串
     */
    @SaCheckPermission("agriculture:materialType:remove")
    @Log(title = "农资类别", businessType = BusinessType.DELETE)
    @DeleteMapping("/{materialTypeIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] materialTypeIds) {
        return toAjax(agricultureMaterialTypeService.deleteWithValidByIds(List.of(materialTypeIds), true));
    }
}
